package com.ailatrieuphu.activity;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.color.Color;

import android.graphics.Typeface;
import android.util.Log;
import android.view.Display;

import com.ailatrieuphu.item.Player;
import com.ailatrieuphu.sprite.PlayerSprite;
import com.ailatrieuphu.util.Config;
import com.ailatrieuphu.util.Util;

public class Play extends BaseGameActivity {
 
	private Camera camera;
	private Scene splashScene;
	private Scene mainScene;
	private BitmapTextureAtlas splashTextureAtlas, playerBitmapTextureAtlas;
	private ITextureRegion splashTextureRegion, playerITextureRegion[];
	private Sprite splash;
	private final float timeSplash = 0.001f;
 
	private PlayerSprite players[];
	private Font mFont;

	private enum SceneType {
		SPLASH, MAIN, OPTIONS, WORLD_SELECTION, LEVEL_SELECTION, CONTROLLER
	}

	public void loadResources() {
		Log.d("-------loadResources()()---------", " ");
		// Load your game resources here!

		this.mFont = FontFactory.create(this.getFontManager(),
				this.getTextureManager(), 256, 256,
				Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
		this.mFont.load();
		playerBitmapTextureAtlas = new BitmapTextureAtlas(getTextureManager(),
				128, 128, TextureOptions.DEFAULT);
		playerITextureRegion = new ITextureRegion[2];
		for (int i = 0; i < playerITextureRegion.length; i++) {
			playerITextureRegion[i] = BitmapTextureAtlasTextureRegionFactory
					.createFromAsset(playerBitmapTextureAtlas, this, "player"
							+ (i + 1) + ".png", i * 48, 0);
		}
		playerBitmapTextureAtlas.load();
	}

	private void loadScenes() {
		Log.d("-------loadScenes()---------", " ");
		mainScene = new Scene();
		// load your game here, you scenes
		mainScene.setBackground(new Background(Color.RED));
		players = new PlayerSprite[2];

		for (int i = 0; i < players.length; i++) {
			final int t = i;
			//Name, gold
//			Player p = new Player("Nguyen Son" + i, 1000);
			players[i] = new PlayerSprite(Util.getRandomIndex(0, 800),
					480 - 48, playerITextureRegion[i],
					getVertexBufferObjectManager());
			players[i].setmFont(mFont);
//			players[i].setPlayer(p);
			players[i].paint();
			mainScene.attachChild(players[i]);
			mainScene.registerTouchArea(players[i]);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private SceneType currentScene = SceneType.SPLASH;

	@Override
	public EngineOptions onCreateEngineOptions() {
		Config.getDisplayScreen(this);
		camera = new Camera(0, 0, Config.SCREEN_WIDTH, Config.SCREEN_HEIGHT);
		EngineOptions engineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
				camera);
		return engineOptions;
	}

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		Log.d("-------onCreateResources()---------", " ");
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		splashTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(),
				256, 256, TextureOptions.DEFAULT);
		splashTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(splashTextureAtlas, this, "player1.png", 0, 0);
		splashTextureAtlas.load();

		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		Log.d("-------onCreateScene()---------", " ");
		initSplashScene();
		pOnCreateSceneCallback.onCreateSceneFinished(this.splashScene);
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		Log.d("-------onPopulateScene()---------", " ");
		mEngine.registerUpdateHandler(new TimerHandler(timeSplash,
				new ITimerCallback() {
					public void onTimePassed(final TimerHandler pTimerHandler) {
						mEngine.unregisterUpdateHandler(pTimerHandler);
						loadResources();
						loadScenes();
						splash.detachSelf();
						mEngine.setScene(mainScene);
						currentScene = SceneType.MAIN;
					}
				}));

		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

	// ===========================================================
	// INITIALIZIE SPLASH SCREEN
	// ===========================================================

	private void initSplashScene() {
		Log.d("-------initSplashScene()---------", " ");
		splashScene = new Scene();
		splash = new Sprite(0, 0, splashTextureRegion,
				mEngine.getVertexBufferObjectManager()) {
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		};

		splash.setScale(1.5f);
		splash.setPosition((Config.SCREEN_WIDTH - splash.getWidth()) * 0.5f,
				(Config.SCREEN_HEIGHT - splash.getHeight()) * 0.5f);
		splashScene.attachChild(splash);
	}

}
